//
//  plViewController.m
//  OpenEyes
//
//  Created by dlios on 15/9/22.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "plViewController.h"
#import "MovieClassModel.h"
#import "MovieClassCollCell.h"
#import "MovieDetaiVC.h"
#import "HTTPTool.h"
#import "VideoVC.h"
#define MovieClassReuse @"movieClassReuse"
@interface plViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property(nonatomic, strong)UICollectionView *movieClassCollView;

@property(nonatomic, strong)NSMutableArray *movieClassArr;


@end

@implementation plViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.movieClassArr = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatTopView];
    [self createDownView];
    [self createMovieClassData];
    [self createMovieClassView];
    //[self buttonCreate];

    // Do any additional setup after loading the view.
}
//-(void)left{
//    [self.navigationController popViewControllerAnimated:YES];
//}

- (void)creatTopView
{
    [super creatTopView];
    [self.leftBuName removeFromSuperview];
    self.rightBu.hidden = YES;
    self.rightBackView.hidden = YES;
}
-(void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}


-(void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)createMovieClassView
{
    
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    flow.itemSize = CGSizeMake(WIDTH / 2 - 2, WIDTH / 2 - 2);
    flow.minimumInteritemSpacing = 4;
    flow.minimumLineSpacing = 4;
    self.movieClassCollView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 113) collectionViewLayout:flow];
    self.movieClassCollView.bounces = NO;
    self.movieClassCollView.showsVerticalScrollIndicator = NO;
    self.movieClassCollView.backgroundColor = [UIColor clearColor];
    self.movieClassCollView.delegate = self;
    self.movieClassCollView.dataSource = self;
    [self.movieClassCollView registerClass:[MovieClassCollCell class] forCellWithReuseIdentifier:MovieClassReuse];
    [self.view addSubview:self.movieClassCollView];
    
}
//-(void)buttonCreate
//{
//    
//    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//    button.frame = CGRectMake(W * 20, H * 500, 30, 30);
//    [button setImage:[UIImage imageNamed:@"iconfont-guanbi-2"] forState:UIControlStateNormal];
//    button.alpha = 0.8;
//    button.layer.masksToBounds = YES;
//    button.layer.borderColor = [[UIColor whiteColor] CGColor];
//    [button addTarget:self action:@selector(left) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:button];
//    
//}
#pragma mark -
#pragma mark 数据解析
- (void)createMovieClassData
{
    NSString *urlStr = @"http://baobab.wandoujia.com/api/v1/categories";
    [HTTPTool get:urlStr body:nil httpResult:JSON success:^(id result) {
        for (NSDictionary *dic in result) {
            MovieClassModel *model = [[MovieClassModel alloc] init];
            
            [model setValuesForKeysWithDictionary:dic];
            [self.movieClassArr addObject:model];
        }
        [self addFileToCacheWithArr:self.movieClassArr key:@"movieClassArr"];
        [self.movieClassCollView reloadData];
    } failure:^(NSError *error) {
        self.movieClassArr = [self getArrFromCacheWithKey:@"movieClassArr"];
        [self.movieClassCollView reloadData];
    }];
}

#pragma mark -
#pragma mark 协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.movieClassArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MovieClassCollCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:MovieClassReuse forIndexPath:indexPath];
    MovieClassModel *model = self.movieClassArr[indexPath.row];
    [cell.backImage sd_setImageWithURL:[NSURL URLWithString:model.bgPicture]];
    cell.titleLabel.text = model.name;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    MovieClassModel *model = self.movieClassArr[indexPath.row];
  VideoVC *dailyVc = [[VideoVC alloc] init];
    dailyVc.classUrlStr = model.name;
    //    dailyVc.modalTransitionStyle = 2;
    //    [self presentViewController:dailyVc animated:YES completion:^{
    
    //    }];
    [self.navigationController pushViewController:dailyVc animated:YES];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
